package com.regentsoft.cloudback.operateAdmin.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.regentsoft.cloudback.base.BaseResp;
import com.regentsoft.cloudback.base.Pager;
import com.regentsoft.cloudback.base.ResponseCode;
import com.regentsoft.cloudback.operateAdmin.entity.Company;
import com.regentsoft.cloudback.operateAdmin.service.CompanyService;
import com.regentsoft.cloudback.vo.ActivationVo;
import com.regentsoft.cloudback.vo.CompanyDetailVo;
import com.regentsoft.cloudback.vo.CompanyVo;
import com.regentsoft.cloudback.vo.CompanyVos;
import com.regentsoft.cloudback.vo.req.CompanyAddReq;
import com.regentsoft.cloudback.vo.req.DeleteComanyReq;
import com.regentsoft.cloudback.vo.req.EditChannelNumReq;
import com.regentsoft.cloudback.vo.req.EditCompanyServiceReq;
import com.regentsoft.cloudback.vo.resp.CompanyAddResp;
import com.regentsoft.cloudback.vo.resp.CompanyDetailResp;
import com.regentsoft.cloudback.vo.resp.CompanyQueryResp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author czd
 *
 * @date 2017年11月25日
 *
 * @version 1.0
 */
@Api
@RestController
@RequestMapping("manager/company/")
public class CompanyController {
	@Autowired
	private CompanyService companyService;
	
	/**
	 * 
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "添加企业", httpMethod = "POST", response = CompanyAddResp.class, notes = "添加企业")
	@RequestMapping(value="addCompany")
    public CompanyAddResp addCompany(@RequestParam String token,@RequestBody CompanyAddReq req)throws Exception {
		CompanyAddResp resp  = new CompanyAddResp();
		
		if(req.getName()==null||req.getCode()==null) {
			resp.setCode(ResponseCode.PARAMETERERR);
			resp.setMessage("填写数据不完整");
			return resp;
		}
		Company company = new Company();	
		company.setId(UUID.randomUUID().toString());
		company.setName(req.getName());
		if(req.getNote()!=null) {
			company.setNote(req.getNote());
		}
		company.setCode(req.getCode());
		company.setCreateTime(new Date());
		
		
        return companyService.initCompanyDB(company,resp);
    }
	
	/**
	 * 企业查询
	 * @param request
	 * @param res
	 * @param member
	 * @param param
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "企业查询", httpMethod = "GET", response = CompanyQueryResp.class, notes = "企业查询")
	@RequestMapping(value="/quaryCompany",method={RequestMethod.GET})
	public CompanyQueryResp quaryCompany(@RequestParam String token,@RequestParam(required=false) String name,
			@RequestParam Integer pageCount,@RequestParam Integer PageSize){
		CompanyQueryResp resp = new CompanyQueryResp();
		try {
			Company company = new Company();
			company.setName(name);
			Pager pager = companyService.findPageList(company, pageCount,PageSize);
			List<Company> list = (List<Company>) pager.getDatas();
			List<CompanyVo> voList = new ArrayList<>();
			CompanyVo vo = null;
			if(list==null||list.size()==0) {
				resp.setCode(ResponseCode.NONE_DATAS);
				resp.setMessage("没有数据！");
				return resp;
			}
			for(Company c:list) {
				vo = new CompanyVo();
				vo.setId(c.getId());
				vo.setCode(c.getCode());
				vo.setName(c.getName());
				vo.setLogisticsMode(c.getLogisticsMode());
				vo.setNormalLogistics(c.getIsNormalLogistics());
				vo.setAppMode(c.getAppMode());
				vo.setChannelNum(c.getChannelNum());
				voList.add(vo);
				
			}
			resp.setTotal(pager.getTotal());
			resp.setData(voList);
			
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		}
		resp.setCode(ResponseCode.SUCCESS);
		return resp;
		
	}
	
	@ApiOperation(value = "删除企业", httpMethod = "POST", response = BaseResp.class, notes = "删除企业")
	@RequestMapping(value="deleteCompany")
    public CompanyAddResp deleteCompany(@RequestParam String token,@RequestBody DeleteComanyReq req)throws Exception {
		CompanyAddResp resp  = new CompanyAddResp();
		
		if(req.getCodes()==null||req.getCodes().size()==0) {
			resp.setCode(ResponseCode.PARAMETERERR);
			resp.setMessage("填写数据不完整");
			return resp;
		}
		int i = companyService.deleteByCodes(req.getCodes());
		if(i>0) {
			resp.setCode(ResponseCode.SUCCESS);
		}else {
			resp.setCode(ResponseCode.FAIL);
		}
        return resp;
    }
	
	@ApiOperation(value = "修改企业服务", httpMethod = "POST", response = BaseResp.class, notes = "修改企业服务")
	@RequestMapping(value="editCompany")
    public CompanyAddResp editCompany(@RequestParam String token,@RequestBody EditCompanyServiceReq req) {
		CompanyAddResp resp  = new CompanyAddResp();
		List<Integer> serviceList = req.getServiceList();
		Company company = new Company();
		company.setId(req.getCompanyId());
		if(req.getServiceList()==null||req.getServiceList().size()==0) {
			resp.setCode(ResponseCode.PARAMETERERR);
			resp.setMessage("填写数据不完整");
			return resp;
		}
		//1.收银 2物流 3.电子面单模式4.寄件码模式
		try {
			if(serviceList.contains(1)&&serviceList.contains(2)) {
				company.setAppMode(0);
			}else {
				if(serviceList.contains(1)) {
					company.setAppMode(2);
				}else if(serviceList.contains(2)) {
					company.setAppMode(1);
				}
			}
			if(serviceList.contains(3)) {
				company.setLogisticsMode(0);
				company.setIsNormalLogistics(1);
			}else if(serviceList.contains(4)) {
				company.setLogisticsMode(1);
				company.setIsNormalLogistics(1);
			}else {
				company.setIsNormalLogistics(0);
			}
			company.setUpdateTime(new Date());
			companyService.update(company);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		}
		resp.setCode(ResponseCode.SUCCESS);
        return resp;
    }
	@ApiOperation(value = "查看企业详情", httpMethod = "GET", response = CompanyDetailResp.class, notes = "查看企业详情")
	@RequestMapping(value="companyDetail")
    public CompanyDetailResp companyDetail(@RequestParam String token,@RequestParam String companyId) {
		CompanyDetailResp resp  = new CompanyDetailResp();

		CompanyVos vos;
		try {
			CompanyDetailVo vo = companyService.companyDetail(companyId);
			vos = new CompanyVos();
			if(vo!=null) {
				vos.setCompanyDetailVo(vo);
			}
			List<ActivationVo> list= companyService.getActivationList(companyId);
			vos.setActivations(list);
			resp.setData(vos);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		}
		resp.setCode(ResponseCode.SUCCESS);
        return resp;
    }
	@ApiOperation(value = "编辑授权店铺数量", httpMethod = "POST", response = BaseResp.class, notes = "编辑授权店铺数量")
	@RequestMapping(value="editChannelNum")
	public BaseResp editChannelNum(@RequestParam String token,@RequestBody EditChannelNumReq req) {
		BaseResp resp  = new BaseResp();
		try {
			companyService.editChannelNum(req.getCompanyId(),req.getChannelNum());
			resp.setCode(ResponseCode.SUCCESS);
		} catch (Exception e) {
			resp.setCode(ResponseCode.FAIL);
			throw new RuntimeException(e.getMessage(),e);
		}
		return resp;
	}
}
